<?php

/*
 * contrat-type/index.phtml
 */
?>
<?php
$title = "Type de Contrat";
$this->headTitle($title);
?>

<div class="content-head">
    <h3>
	<?php echo $title; ?>
	<img src='<?php echo $this->basePath() . '/img/rhx/refresh-16x16.png' ?>' title="Actualiser la liste" class='rhx-refresh'>
    </h3>
    <div class="toolbar">
	<div class="toggle add" title="Ajouter">
            <a href="#" style="display: block; width: 100%; height: 100%;" onclick="$('.add-toggle').toggle('slow'); return false;"></a>
        </div>
    </div>
</div>

<div class="content-body">
    <!-- AJOUTER NOUVEAU -->
    <div class="add-toggle">
	<div class="list-container">
	    <table class="table table-striped list-table">
		<thead>
		    <tr>
			<td width="50%">Libellé</td>
			<td width="20%">Abréviation</td>
			<td width="20%">Coefficient</td>
			<td width="10%">&nbsp;</td>
		    </tr>
		</thead>
		<tbody>
		    <tr>
			<td>
			    <a href="#" id="libelleNew" data-type="text"></a>
			</td>
			<td>
			    <a href="#" id="abrevNew" data-type="text"></a>
			</td>
			<td>
			    <a href="#" id="coefNew" data-type="text">0.0</a>
			</td>
			<td>
			    <button class="btn btn-success add-new-type" id="data-validate">Ajouter</button>
			</td>
		    </tr>
		</tbody>
	    </table>
	</div>
	<form id="formNew" action="#" method="post">
	    <input type="hidden" id="data-id" name="id" value="0">
	    <input type="hidden" id="data-libelle" name="libelle" value="">
	    <input type="hidden" id="data-abrev" name="abrev" value="">
	    <input type="hidden" id="data-coef" name="coef" value="0.0">
	</form>
    </div>

    <div id="content-data">
	<!-- AJAX CONTENT -->
    </div>
</div>
<script type="text/javascript">
    // Démarrage
    $(function(){
	// Editable
	$('#libelleNew').editable({
	    title: "Nouveau Libellé",
	    validate: function(value){
		value = $.trim(value);
		$('#data-libelle').val(value);
	    }
	});
	$('#abrevNew').editable({
	    title: "Nouvelle Abréviation",
	    validate: function(value){
		value = $.trim(value);
		$('#data-abrev').val(value);
	    }
	});
	$('#coefNew').editable({
	    title: "Nouveau Coefficient",
	    validate: function(value){
		value = $.trim(value);
		$('#data-coef').val(value);
	    }
	});
	// Ajouter
	$('.add-new-type').click(function(e){
	    e.preventDefault();
	    libelle = $('#data-libelle').val();
	    abrev = $('#data-abrev').val();
	    coef = $('#data-coef').val();
	    if(libelle.length < 1){
		alert("Le libellé est trop court ou il est Vide !");

		return false;
	    }
	    if(abrev.length < 1){
		alert("L'abréviation est trop court ou il est Vide !");

		return false;
	    }
	    coef = parseFloat(coef);
	    if(coef <= 0){
		alert("Le coefficient ne peut pas être Inférieur ou égale à zéro");

		return false;
	    }

	    ajax = $.ajax({
		type: 'post',
		url : '<?php echo $this->url('RHX/default', array('controller'	 => 'contrattype', 'action'	 => 'add')); ?>',
		data: $('#formNew').serialize()
	    });
	    ajax.done(function(data){
		refresh();
	    });
	    ajax.fail(function(){
		alert("Une erreur est survenue pendant l'ajout");
	    });
	});
	$('.rhx-refresh').click(function(e){
	    e.preventDefault();
	    refresh();
	});

	// rafraichissement
	refresh();
    });
    // Fonction de rafraichissement
    function refresh(){
	ajax = $.ajax({
	    type: 'post',
	    url : '<?php echo $this->url('RHX/default', array('controller'	 => 'contrattype', 'action'	 => 'loadIndex')); ?>',
	});
	ajax.done(function(data){
	    $('#content-data').hide().html(data).fadeIn('normal');
	});
	ajax.fail(function(){
	    $('#content-data').hide().html("<div class='rh-error'>Contenu Impossible à charger</div>").fadeIn('normal');
	});
    }
</script>